package class03;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 利用队列实现，先放头，然后放左右
 */
public class Code03_LevelTraversalBT {

    static class Node {
        int value;
        Node left;
        Node right;

        Node(int value) {
            this.value = value;
        }
    }


    static void level(Node node) {
        if (null == node) {
            return;
        }

        Queue<Node> queue = new LinkedList<>();

        queue.offer(node);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.print(cur.value + " ");

            if (null != cur.left) {
                queue.offer(cur.left);
            }

            if (null != cur.right) {
                queue.offer(cur.right);
            }
        }
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);
        level(head);
    }
}
